<script type="text/javascript">
	window._AMapSecurityConfig = {
		securityJsCode:'{$js_code_amap}',
	}
</script>
<div id="pstype2_div">
	<div class="layui-form-item">
		<label class="layui-form-label">配送费：</label>
		<div class="layui-input-inline" style="width: 100px;">
			<input type="text" name="info[peisong_juli1]" class="layui-input" value="{$config_arr.peisong_juli1}">
		</div>
		<div class="layui-form-mid">公里内</div>
		<div class="layui-input-inline" style="width: 100px;">
			<input type="text" name="info[peisong_fee1]" class="layui-input" value="{$config_arr.peisong_fee1}">
		</div>
		<div class="layui-form-mid">元；每超出</div>
		<div class="layui-input-inline" style="width: 100px;">
			<input type="text" name="info[peisong_juli2]" class="layui-input" value="{$config_arr.peisong_juli2}">
		</div>
		<div class="layui-form-mid">公里，加</div>
		<div class="layui-input-inline" style="width: 100px;">
			<input type="text" name="info[peisong_fee2]" class="layui-input" value="{$config_arr.peisong_fee2}">
		</div>
		<div class="layui-form-mid">元</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">配送范围：</label>
		<div style="display: flex;flex-direction: column;">

			<div class="adressDiv" style="margin-bottom: 10px">
				<div class="layui-input-inline" style="width:300px">
					<input type="radio" name="info[peisong_rangetype]"  value="0" title="圆形范围" {if !$config_arr || ($info['pstype']==2 && $config_arr['peisong_rangetype']==0)}checked{/if} lay-filter="rangetypeset">
					<input type="radio" name="info[peisong_rangetype]"  value="1" title="多边形范围" {if $config_arr && $info['pstype']==2 && $config_arr['peisong_rangetype']==1}checked{/if} lay-filter="rangetypeset">
				</div>
			</div>
			<div class="areaDiv" style="margin-bottom: 8px">
				<div class="layui-input-inline">
					<select name="info[province]" id="cat1" lay-filter="cat1">
						<option value="">请选择</option>
					</select>
				</div>
				<div class="layui-input-inline">
					<select name="info[city]" id="cat2" lay-filter="cat2">
						<option value="">请选择</option>
					</select>
				</div>
				<div class="layui-input-inline">
					<select name="info[county]" id="cat3" lay-filter="cat3">
						<option value="">请选择</option>
					</select>
				</div>
				<div class="layui-input-inline" style="white-space: nowrap;">
					切换省市区可移动配送范围中心点
				</div>
			</div>
		</div>

		<div class="form-group" style="margin-left:130px;padding-top:10px;padding-bottom:5px">
			<script src="https://webapi.amap.com/maps?v=1.4.0&key={$map_key_amap}&plugin=AMap.PolyEditor,AMap.CircleEditor,AMap.Geocoder"></script>
			<div id="container" class="map" style="width:800px;height: 600px;"></div>
		</div>
		<div id="rangetypeset0">
			<div class="layui-form-item">
				<div class="layui-form-mid" style="margin-left:130px;">中心点坐标</div>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" name="info[peisong_lng]"  class="layui-input" value="{$config_arr.peisong_lng}">
				</div>
				<div class="layui-form-mid">,</div>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" name="info[peisong_lat]"  class="layui-input" value="{$config_arr.peisong_lat}">
				</div>
				<div class="layui-form-mid">配送半径</div>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" name="info[peisong_range]" class="layui-input" value="{$config_arr.peisong_range}">
				</div>
				<div class="layui-form-mid">米</div>
			</div>
		</div>
		<div id="rangetypeset1">
			<input type="hidden" name="info[peisong_rangepath]" class="layui-input" value="{$config_arr.peisong_rangepath}">
			<div class="layui-form-mid" style="margin-left:130px;">注：多边形的形状尽量简洁，不要有交叉线</div>

			<div class="layui-form-item">
				<label class="layui-form-label">配送点：</label>
				<div class="layui-input-inline" style="width:150px">
					<input type="text" name="info[peisong_lng2]" value="{$config_arr['peisong_lng2']}" class="layui-input">
				</div>
				<div class="layui-form-mid">-</div>
				<div class="layui-input-inline" style="width:150px">
					<input type="text" name="info[peisong_lat2]" value="{$config_arr['peisong_lat2']}" class="layui-input">
				</div>
				<button type="button" class="layui-btn" onclick="choosezuobiao()" style="float:left">选择坐标</button>
			</div>

		</div>
	</div>
</div>

	<div class="layui-form-item" id="pstype02_div">
		<label class="layui-form-label">满额包邮：</label>
		<div class="layui-input-inline" style="width:70px">
			<input type="checkbox" name="info[freeset]" value="1" lay-text="开启|关闭" lay-skin="switch" lay-filter="freeset" {if !empty($config_arr['freeset']) && $config_arr['freeset']==1}checked{/if}>
		</div>
		<div id="freesetdiv"  {if empty($config_arr['freeset']) || $config_arr['freeset']!=1}style="display:none;"{/if}>
			<div class="layui-form-mid">满</div>
			<div class="layui-input-inline" style="width:70px"><input type="text" name="info[free_price]" value="{$config_arr.free_price}"   class="layui-input"></div>
			<div class="layui-form-mid">元包邮</div>
		</div>
	</div>

	<div class="layui-form-item" id="pstype12_div">
		<label class="layui-form-label">满额起送：</label>
		<div class="layui-input-inline" style="width:70px">
			<input type="checkbox" name="info[minpriceset]" value="1"   lay-text="开启|关闭" lay-skin="switch" lay-filter="minpriceset" {if !empty($config_arr['minpriceset']) && $config_arr['minpriceset']==1}checked{/if}>
		</div>
		<div id="minpricesetdiv"  {if empty($config_arr['minpriceset']) || $config_arr['minpriceset']!=1}style="display:none;"{/if}>
			<div class="layui-form-mid">满</div>
			<div class="layui-input-inline" style="width:70px"><input type="text" name="info[minprice]" value="{$config_arr.minprice}" class="layui-input"  ></div>
			<div class="layui-form-mid">元起送</div>
		</div>
	</div>

	<div class="layui-form-item" id="pstype13_div">
		<label class="layui-form-label">满件起送：</label>
		<div class="layui-input-inline" style="width:70px">
			<input type="checkbox" name="info[minnumset]" value="1" lay-text="开启|关闭" lay-skin="switch" lay-filter="minnumset" {if !empty($config_arr['minnumset']) && $config_arr['minnumset']==1}checked{/if}>
		</div>
		<div id="minnumsetdiv" {if empty($config_arr['minnumset']) || $config_arr['minnumset']!=1}style="display:none;"{/if}>
			<div class="layui-form-mid">满</div>
			<div class="layui-input-inline" style="width:70px"><input type="text" name="info[minnum]" value="{$config_arr.minnum}" class="layui-input"  ></div>
			<div class="layui-form-mid">件起送</div>
		</div>
	</div>


	<script src="/static/admin/js/cat_select.js?s=345"></script>
	<script>
		cat_select('cat1','cat2','cat3',"{$config_arr.province}","{$config_arr.city}","{$config_arr.county}","{:url('Tools/getRegion')}");
	</script>

	<script>

	layui.form.on('switch(freeset)',function(data){
		if(data.elem.checked){
			$('#freesetdiv').show();
		}else{
			$('#freesetdiv').hide();
		}
	})

	layui.form.on('switch(minpriceset)',function(data){
		if(data.elem.checked){
			$('#minpricesetdiv').show();
		}else{
			$('#minpricesetdiv').hide();
		}
	})
	layui.form.on('switch(minnumset)',function(data){
		if(data.elem.checked){
			$('#minnumsetdiv').show();
		}else{
			$('#minnumsetdiv').hide();
		}
	})
  </script>

	<script>
	var lng = $("input[name='info[peisong_lng]']").val();
	var lat = $("input[name='info[peisong_lat]']").val();
	var range = $("input[name='info[peisong_range]']").val();
	var rangetype = '{$config_arr.peisong_rangetype}';
	var rangepath = $("input[name='info[peisong_rangepath]']").val();
	var lnglat = '';//设置的坐标
	if(lng !="" && lat !=""){ 
		lng = parseFloat(lng);
		lat = parseFloat(lat);
		lnglat=[lng,lat];
	}

	if(range==0){ range=2000;}

	layui.form.on('radio(rangetypeset)',function(data){
		var lng = $("input[name='info[peisong_lng]']").val();
		var lat = $("input[name='info[peisong_lat]']").val();
		new_lnglat = [parseFloat(lng),parseFloat(lat)];
		if(data.value == '0'){
			$('#rangetypeset0').show();
			$('#rangetypeset1').hide();
			addcircle(new_lnglat);
		}else{
			$('#rangetypeset0').hide();
			$('#rangetypeset1').show();
			console.log(new_lnglat);
			addpoly(new_lnglat);
		}
	})
	//初始化地图参数
	var map = new AMap.Map("container", {
		resizeEnable: true,//是否监控地图容器尺寸变化，默认值为false
		dragEnable: true,//是否允许拖拽地图
		keyboardEnable: false,//是否允许键盘平移
		doubleClickZoom: false,//是否允许双击放大地图
		scrollWheel:true,//是否允许鼠标滚轮操作地图
		center:lnglat,
		zoom: 13 //地图显示的缩放级别
	});

	var mapcenter = map.getCenter();
	var lng = mapcenter.lng
	var lat = mapcenter.lat
	lnglat = [lng,lat];
	$("input[name='info[peisong_lng]']").val(lng);
	$("input[name='info[peisong_lat]']").val(lat);
	$("input[name='info[peisong_range]']").val(range);

	if(!rangepath){
		var key = 0.02;
		rangepath = [
			[lng - key, lat - key],
			[lng + key, lat - key],
			[lng + key, lat + key],
			[lng - key, lat + key]
		];
	}else{
		rangepathdata = rangepath.split(';');
		rangepath = [];
		for(var i in rangepathdata){
			var path = rangepathdata[i].split(',');
			rangepath.push([path[0],path[1]]);
		}
		console.log(rangepath);
	}
	if(rangetype==0 ){
		addcircle();
	}else{
		addpoly();
	}
	function addcircle(new_lnglat){
		var center_lnglat = lnglat;
		if(new_lnglat){
			center_lnglat = new_lnglat;
			$("input[name='info[peisong_lng]']").val(new_lnglat[0]);
			$("input[name='info[peisong_lat]']").val(new_lnglat[1]);
		}
		map.clearMap();
		var circlegon = new AMap.Circle({
			center: center_lnglat,// 圆心位置
			radius: range, //半径
			strokeColor: "#4e73f1", //线颜色
			strokeOpacity: 1, //线透明度
			strokeWeight: 3, //线粗细度
			fillColor: "#4e73f1", //填充颜色
			fillOpacity: 0.35,//填充透明度
		});

		map.add(circlegon)
		map.setFitView([circlegon]);

		var circleEditor= new AMap.CircleEditor(map,circlegon);
		circleEditor.open(lnglat);
		
		circleEditor.on('move', function (event) {
			$("input[name='info[peisong_lng]']").val(event.lnglat.lng);
			$("input[name='info[peisong_lat]']").val(event.lnglat.lat);
		});
		circleEditor.on('adjust', function (event) {
			$("input[name='info[peisong_range]']").val(event.radius);
		});
	}
	function addpoly(new_lnglat){
		if(new_lnglat){
			var key = 0.02;
			rangepath = [
				[new_lnglat[0] - key, new_lnglat[1]  - key],
				[new_lnglat[0]  + key, new_lnglat[1] - key],
				[new_lnglat[0]  + key, new_lnglat[1] + key],
				[new_lnglat[0]  - key, new_lnglat[1] + key]
			];
		}
		map.clearMap();
		var polygon = new AMap.Polygon({
			path: rangepath,
			strokeColor: "#FF33FF",
			strokeOpacity: 0.2,
			fillOpacity: 0.4,
			fillColor: '#1791fc',
			zIndex: 50,
			draggable:true
		});
		map.add(polygon)
		map.setFitView([polygon])
		var polyEditor = new AMap.PolyEditor(map, polygon);
		polyEditor.open();
		setpathvalue(polygon);
		polyEditor.on('addnode', function (event) {
			console.log('触发事件：addnode')
			setpathvalue(polygon);
		});
		polyEditor.on('adjust', function (event) {
			console.log('触发事件：adjust');
			setpathvalue(polygon);
		});
		polyEditor.on('removenode', function (event) {
			console.log('触发事件：removenode')
			setpathvalue(polygon);
		});
		polyEditor.on('end', function (event) {
			console.log('触发事件： end')
			setpathvalue(polygon);
		});
		polygon.on('change', function (event) {
			console.log('polygon触发事件： change')
			setpathvalue(polygon);
		});
	}
	function setpathvalue(polygon){
		var pathdata = polygon.getPath();
		var pathArr = [];
		for(var i in pathdata){
			pathArr.push(pathdata[i].lng +','+pathdata[i].lat);
		}
		$("input[name='info[peisong_rangepath]']").val(pathArr.join(';'));
	}

	function choosezuobiao(){
		var address = '';
		var longitude = $("input[name='info[peisong_lng2]']").val();
		var latitude = $("input[name='info[peisong_lat2]']").val();
		if(!longitude){
			longitude = $("input[name='info[peisong_lng]']").val();
			latitude = $("input[name='info[peisong_lat]']").val();
		}
		var choosezblayer = layer.open({type:2,shadeClose: true,area: ['800px', '560px'],'title': '选择坐标',content: "{:url('DesignPage/choosezuobiao')}?address="+(address?address:"")+"&jd="+(longitude?longitude:"")+"&wd="+(latitude?latitude:""),btn:['确定','取消'],yes:function(index, layero){
			var longitude = layero.find('iframe').contents().find('#mapjd').val();
			var latitude = layero.find('iframe').contents().find('#mapwd').val();
			$("input[name='info[peisong_lng2]']").val(longitude);
			$("input[name='info[peisong_lat2]']").val(latitude);
			layer.close(choosezblayer);
		}});
	}
	</script>
